/**************************************************************************
** Yabet is bloging editor tool that supports many blog's formats. It is very usefull for crossposting, allows easily to
** make posts to your blogs.
**
** Copyright (C) 2010 Valery Konchin <eeexception@gmail.com>
**
** This program is free software: you can redistribute it and/or modify
**     it under the terms of the GNU General Public License as published by
**     the Free Software Foundation, either version 3 of the License, or
**     (at your option) any later version.
**
**     This program is distributed in the hope that it will be useful,
**     but WITHOUT ANY WARRANTY; without even the implied warranty of
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**     GNU General Public License for more details.
**
**     You should have received a copy of the GNU General Public License
**     along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#include "screenshotdialog.h"
#include "ui_screenshotdialog.h"
#include <QPixmap>
#include <QDesktopWidget>
#include <QDir>
#include <QDateTime>

ScreenshotDialog::ScreenshotDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ScreenshotDialog)
{
    ui->setupUi(this);

    ui->imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    timer = new QTimer(this);
    lcdTimer = new QTimer(this);

    connect(ui->screenshotPushButton, SIGNAL(pressed()), this, SLOT(screenshotButtonPressed()));
    connect(timer, SIGNAL(timeout()), this, SLOT(takeScreenShot()));
    connect(lcdTimer, SIGNAL(timeout()), this, SLOT(lcdChange()));
}

ScreenshotDialog::~ScreenshotDialog()
{
    delete ui;
}

void ScreenshotDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ScreenshotDialog::lcdChange() {
    ui->lcdNumber->display(ui->lcdNumber->value() - 1);
}

void ScreenshotDialog::screenshotButtonPressed() {
    ui->lcdNumber->display(9);
    timer->start(10000);
    lcdTimer->start(1000);
}

void ScreenshotDialog::takeScreenShot() {
    lcdTimer->stop();
    timer->stop();

    originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());

    ui->imageLabel->setPixmap(originalPixmap.scaled(ui->imageLabel->size(),
                                                    Qt::KeepAspectRatio, Qt::SmoothTransformation));

    tmpFileName = QDir::tempPath() + QDir::separator() + "tmpScreenShot" + QDateTime::currentDateTime().toString() + ".png";
    originalPixmap.save(tmpFileName, QString("png").toAscii());
}

QString ScreenshotDialog::getTmpImage() {
    return tmpFileName;
}
